AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )


include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.vent = 0
	self.sounded = 0
	self.dir = Vector(0,0,0)
	self.damaged = 0
	LS_RegisterEnt(self.Entity)

	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "Vent Container" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "Vent Container") then
		if (value ~= 0) then
			if ( self.vent == 0 ) then
				self.vent = 1
			end
		else
			if ( self.vent == 1 ) then
				self.vent = 0
			end
		end
	end
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
		self.Entity:EmitSound( "PhysicsCannister.ThrusterLoop" )
		self.dir = Vector(math.random(-999999, 999999), math.random(-999999, 999999), math.random(-999999, 999999))
		
	end
end

function ENT:Repair()
	self.health = self.max_health
	self.damaged = 0
	self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)
		if (self.environment.atmosphere > 0) then 
			util.BlastDamage(self.Entity,self.Entity,self.Entity:GetPos(),math.Round(math.sqrt(RD_GetResourceAmount(self, "hydrogen"))),math.Round((RD_GetResourceAmount(self, "hydrogen")/2)*self.environment.atmosphere))--BOOM!
		end
	
	self.Entity:Remove()
end


function ENT:Output()
	return 1
end

function ENT:ShowOutput(value)
	self:SetOverlayText( "Hydrogen\n(" .. value .. ")" )
	return value
end

function ENT:Think()
	self:ShowOutput(self.val1)
	if ( self.timer > CurTime() ) then return end
	local hydrogen = RD_GetResourceAmount(self, "hydrogen")
	
	self.val1 = hydrogen
	
	if self.vent == 1 then --MAYDAY! VENT THE CONTAINER! (lol)
		RD_ConsumeResource(self, "hydrogen", self.val1);
		if self.sounded == 0 then
			self.Entity:EmitSound( "PhysicsCannister.ThrusterLoop" )
			self.Entity:EmitSound( "ambient.steam01" )
			self.sounded = 1
		end
	elseif (self.sounded == 1 and self.vent == 0) then
		self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
		self.Entity:StopSound( "ambient.steam01" )
		self.sounded = 0
	end
	
	if ((self.damaged == 1) and (hydrogen > 0)) then
		local hydrogen = RD_GetResourceAmount(self, "hydrogen")
		local rnd = math.random(0,150)
		local dir = Vector(0,0,0)
		if (hydrogen >= rnd) then
			RD_ConsumeResource(self, "hydrogen", rnd)
			self.Entity:GetPhysicsObject():ApplyForceOffset(Vector(0,0,rnd*1000), self.dir)
		else
			RD_ConsumeResource(self, "hydrogen", hydrogen)
			self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )
			self.Entity:GetPhysicsObject():ApplyForceOffset(Vector(0,0,hydrogen*1000), self.dir)
		end
	end
	
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", hydrogen) end
	self.timer = CurTime() +  1
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
